package com.isoft.controller;

import com.isoft.pojo.Person;
import com.isoft.service.impl.AddressBooksServiceImpl;
import com.isoft.service.impl.IAddressDookService;
import com.isoft.utils.SystemMenu;

import java.util.Scanner;

public class AddressBookContorller {
    Scanner input;
    IAddressDookService addressDookService;

    public AddressBookContorller() {
        input = new Scanner(System.in);
        addressDookService = new AddressBooksServiceImpl();
    }

    public void startSystem() {
        try {
            do {
                SystemMenu.printSystemMenu();
                String op = input.next();
                switch (op) {
                    case "1" -> {
                        Person person = new Person();
                        System.out.println("正在执行添加通讯录功能...");
                        System.out.println("--------------------");
                        System.out.println("请输入商品名称：");
                        String name = input.next();
                        System.out.println("请输入商品价格：");
                        String price = input.next();
                        System.out.println("请输入商品库存：");
                        String quantity = input.next();
                        System.out.println("请输入商品编号");
                        String id = input.next();
                        person.setName(name);
                        person.setId(Integer.parseInt(id));
                        person.setPrice(Double.parseDouble(price));
                        person.setQuantity(Integer.parseInt(quantity));
                        insert(person);
                    }
                    case "2" -> {
                        Person person = new Person();
                        //先查再改,根据XXXX修改
                        System.out.println("正在执行修改通讯录功能...");
                        System.out.println("--------------------");
                        System.out.println("请输入商品名称：");
                        String name = input.next();
                        int i = findByName(name);
                        if (i == 1) {
                            System.out.println("请输入商品价格：");
                            String price = input.next();
                            System.out.println("请输入商品库存：");
                            String quantity = input.next();
                            System.out.println("请输入商品编码");
                            String id = input.next();
                            person.setName(name);
                            person.setId(Integer.parseInt(id));
                            person.setPrice(Double.parseDouble(price));
                            person.setQuantity(Integer.parseInt(quantity));
                            updateByName(person);
                        }

                    }
                    case "3" -> {
                        System.out.println("请输入要删除的商品名称：");
                        String name = input.next();
                        deleteByName(name);
                    }
                    case "4" -> {
                        System.out.println("请输入要查询的商品名称：");
                        String name = input.next();
                        findByName(name);
                    }
                    case "5" -> {
                        exit();
                    }
                    default -> {
                        System.out.println("您的输入有误！");
                    }
                }
                System.out.println("按任意键继续...");
                System.in.read();
            } while (true);
        } catch (Exception e) {

        }
    }

    public void insert(Person person) {
//        System.out.println("将执行添加功能...");
        int result = addressDookService.insert(person);
        if (result == 1) {
            System.out.println("添加商品成功!");
        } else {
            System.out.println("添加商品失败！");
        }
    }

    public void deleteByName(String name) {
        int i = addressDookService.deleteByName(name);
        if (i > 0) {
            System.out.println("删除成功！");
        } else {
            System.out.println("查无此物！");
        }
    }

    public void updateByName(Person person) {
        int i = addressDookService.updateByName(person);
        if (i >= 1) {
            System.out.println("修改商品成功!");
        } else {
            System.out.println("修改商品失败！");
        }
    }

    public void findAll() {

    }

    public int findByName(String name) {
        Person person = addressDookService.findByName(name);
        if (person != null) {
            System.out.println(person);//制作一个打印的格式菜单
            return 1;
        } else {
            System.out.println("查无此物！");
            return 0;
        }
    }

    public void exit() {

    }
}
